package com.wyu.servlet;

import com.wyu.service.YGService;
import com.wyu.service.YGServiceImpl;
import com.wyu.util.DesUtil;
import com.wyu.util.Md5Password;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ygh = request.getParameter("ygh");
        String xm = request.getParameter("xm");
        String mm = request.getParameter("mm");
        String xb = request.getParameter("xb");
        String zw = request.getParameter("zw");
        String bz = request.getParameter("bz");
        System.out.println("工号:" + ygh + ",姓名:" + xm +",密码:" + mm + ",性别:" + xb +",职位:"+ zw + ",备注:" +bz);

        try{
            DesUtil des = new DesUtil("Java");
            String mm3 = des.encrypt(mm);//DES加密
            System.out.println("需要DES加密前的密码:" + mm);
            System.out.println("DES加密后的密码:" + mm3 );
            YGService ygService = new YGServiceImpl();
            int yg = ygService.register(ygh,xm,mm3,xb,zw,bz);
            if (yg>0){
                response.sendRedirect("register.jsp");
                request.getSession().setAttribute("RegisterMsg","添加成功");
            }else {
                request.getSession().setAttribute("RegisterMsg","该员工已经存在，不能重复添加！");
                response.sendRedirect("register.jsp");
            }

        }catch (Exception e){
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
